
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/admin/css/reset.css" />
    </head>
    <body class="pear-container">

        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">

                    <div class="layui-form-item">
                        <label class="layui-form-label">任务名称</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block">
                                <input type="hidden" autocomplete="off" name="job_name[]" value="like" class="layui-input inline-block">
                                <input type="text" autocomplete="off" name="job_name[]" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>

        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="admin.job.insert">
                <i class="layui-icon layui-icon-add-1"></i>新增
            </button>
            <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="admin.job.delete">
                <i class="layui-icon layui-icon-delete"></i>删除
            </button>
        </script>

        <!-- 表格行工具栏 -->
        <script type="text/html" id="table-bar">
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="admin.job.update">编辑</button>
            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="admin.job.delete">删除</button>
            <button class="pear-btn pear-btn-sm" id="more_{{d.id}}"><i class="layui-icon layui-icon-triangle-d"></i></button>
        </script>

        <script src="/component/layui/layui.js?v=2.8.12"></script>
        <script src="/component/pear/pear.js"></script>
        <script src="/admin/js/permission.js"></script>
        <script src="/admin/js/common.js"></script>
        <script src="/admin/js/soulTable.js"></script>

        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/admin/job/select";
            const UPDATE_API = "/admin/job/update";
            const DELETE_API = "/admin/job/delete";
            const INSERT_URL = "/admin/job/insert";
            const UPDATE_URL = "/admin/job/update";

            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util","soulTable","drawer","dropdown"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                let soulTable = layui.soulTable;
                let drawer = layui.drawer;
                let dropdown = layui.dropdown;

				// 表头参数
				let cols = [
					{
						type: "checkbox",
						align: "center"
					},{
						title: "任务ID",align: "center",
						field: "id",
					},{
						title: "任务名称",align: "center",
						field: "job_name",
					},{
						title: "任务组名",align: "center",
						field: "job_group",
					},{
						title: "调用目标字符串",align: "center",
						field: "invoke_target",
					},{
						title: "cron执行表达式",align: "center",
						field: "cron_expression",
					},{
						title: "计划执行错误策略（1立即执行 2执行一次 3放弃执行）",align: "center",
						field: "misfire_policy",
					},{
						title: "是否并发执行（0允许 1禁止）",align: "center",
						field: "concurrent",
					},{
						title: "状态（0正常 1暂停）",align: "center",
						field: "status",
					},{
						title: "备注信息",align: "center",
						field: "remark",
					},{
						title: "sort",align: "center",
						field: "sort",
					},{
						title: "添加人",align: "center",
						field: "created_by",
						hide: true,
					},{
						title: "创建时间",align: "center",
						field: "created_at",
						hide: true,
					},{
						title: "修改时间",align: "center",
						field: "updated_at",
						hide: true,
					},{
						title: "修改人",align: "center",
						field: "updated_by",
						hide: true,
					},{
						title: "操作",
						toolbar: "#table-bar",
						align: "center",
						fixed: "right",
						width: 200,
					}
				];

				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API+"?order=asc&field=sort",
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				           rowDrag: {done: function(obj) {
				            const row_id = obj.row.id;  	// 当前id
				            const row_sort = obj.newIndex + 1; 		// 拖动后的排序
				            $.ajax({
				                url:UPDATE_API,
				                type:'POST',
				                dataType:'JSON',
				                data:{id:row_id,sort:row_sort},
				                success:function (res) {
				                    layer.msg("操作成功", {
				                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
				                    }, function(){
				                        //do something
				                        location.reload()
				                    });
				                }
				            })
				        }},
				    done: function (res, curr, count) {
				        //layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				        soulTable.render(this);
				        for (let i = 0; i < res.data.length; i++) {
				                dropdown.render({
				                    elem: "#more_" + res.data[i].id,
				                    data: [{
				                        title: "按钮名称",
				                        id: "button_name"},
				                        ],
				                    id: "#more_" + res.data[i].id,
				                    click: function(obj) {
				                        const id = res.data[i].id;
				                        if (obj.id === "button_name") {
				                            //方法需要自己去实现
                                            layer.msg("方法未实现");
				                            //button_name(id);
				                        }
				                    }
				                });
				            }
				    }
				});


                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });

                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });

                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                     drawer.open({
                        legacy: false,
                        title: ['新增', 'font-size:16px;color:#2d8cf0'],
                        offset: 'r',
                        area: "50%",
                        maxmin: true,
                        closeBtn: 1,
                        iframe: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    drawer.open({
                        legacy: false,
                        title: ['编辑', 'font-size:16px;color:#2d8cf0'],
                        offset: 'r',
                        area: "50%",
                        maxmin: true,
                        closeBtn: 1,
                        iframe: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function() {
                    table.reloadData("data-table", {
                        scrollPos: "fixed",
                        done: function (res, curr) {
                            if (curr > 1 && res.data && !res.data.length) {
                                curr = curr - 1;
                                table.reloadData("data-table", {
                                    page: {
                                        curr: curr
                                    },
                                })
                            }
                        }
                    });
                }
            })

        </script>
    </body>
</html>
